<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>系统功能</el-breadcrumb-item>
				<el-breadcrumb-item>编辑地区</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		
		<div class="content-box">
			<el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
				<el-tabs v-model="activeName" type="card">
					<el-tab-pane label="基本信息" name="info">
						<div class="tab-content">
							<el-form-item prop="parentId" label="所属地区">
								<dt-dropdown-select v-model="model.parentId"
									:disabled="model.id"
									:data="listData"
									placeholder="请选择">
								</dt-dropdown-select>
							</el-form-item>
							<el-form-item prop="title" label="地区名称">
								<el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
							</el-form-item>
							<el-form-item prop="sortId" label="排序数字">
								<el-input v-model="model.sortId" placeholder="数字越小越排前"></el-input>
							</el-form-item>
						</div>
					</el-tab-pane>
				</el-tabs>
			</el-form>
		</div>
		
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	import DtDropdownSelect from '../../../components/DtDropdownSelect.vue'
	
	export default {
		components: {
			DtDropdownSelect
		},
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				listData: [],
				model: {
					id: 0,
					parentId: 0,
					title: null,
					sortId: 99
				},
				rules: {
					title: [
						{ required: true, message: '请输入地区名称', trigger: 'blur' }
					],
					sortId: [
						{ required: true, message: '请输入排序数字', trigger: 'blur' },
						{ pattern: /^[+]{0,1}(\d+)$/, message: '只能为正整数', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			//加载列表
			loadData(){
				let _this = this; //当前页面
				_this.$api.request({
					url: '/admin/area',
					loading: true,
					success(res) {
						_this.listData = res.data
					}
				});
			},
			//初始化数据
			initData() {
				let _this = this; //当前页面
				let parentId = _this.$route.query.parentId; //获取父ID
				if (_this.id) {
					_this.$api.request({
						url: '/admin/area/' + _this.id,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						}
					});
				} else if (parentId) {
					_this.model.parentId = parseInt(parentId);
				}
				//加载列表
				_this.loadData();
			},
			//提交表单
			submitForm() {
				let _this = this; //当前页面
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm'].validate((valid) => {
						if (valid) resolve()
					});
				});
				Promise.all([f1]).then(() => {
					//如果是数组则取最后一个值
					if (Array.isArray(_this.model.parentId)) {
						_this.model.parentId = _this.model.parentId[_this.model.parentId.length - 1];
					}
					//提交表单,Id大于0则修改，否则添加
					if (_this.model.id > 0) {
						_this.$api.request({
							method: 'put',
							url: '/admin/area/' + _this.model.id,
							data: _this.model,
							loading: true,
							successMsg: '修改地区已成功',
							success: function (res) {
								//可跳转加列表页
								_this.$common.linkUrl('/setting/area/list');
							}
						});
					} else {
						_this.$api.request({
							method: 'post',
							url: '/admin/area',
							data: _this.model,
							loading: true,
							successMsg: '新增地区已成功',
							success(res) {
								_this.loadData();
								//重置表单
								_this.$refs['editForm'].resetFields();
							}
						});
					}
				})
			},
		},
		created() {
			this.initData();
		}
	}
</script>

<style lang="scss">
</style>